<?php

// +----------------------------------------------------------------------
// | 分类控制器
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\app\admin;

use app\common\event\SystemConfig as SystemConfigEvent;
use app\common\model\SystemConfig as SystemConfigModel;
use yicmf\builder\Edit;
use yicmf\builder\Table;
use think\facade\Url;
use app\controller\admin\Common;
use app\model\app\ExchangeCoupon as ExchangeCouponModel;


class ExchangeCoupon extends Common
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function _filter()
    {
    }

    public function index()
    {

        $statusOptions = [
            [
                'id' => 1,
                'value' => '信用卡'
            ],
            [
                'id' => 2,
                'value' => '贷款'
            ],
            [
                'id' => 3,
                'value' => '租机'
            ]
        ];
        //读取规则列表
        $map = $this->_search($this->dbname);
        //            //显示页面
        $builder = new Table();
        $builder->model(ExchangeCouponModel::class)
            ->where($map)
            ->order('id DESC,name ASC')
            ->setSearchPostUrl('', '')
            ->searchTextLike('name', '产品名称：')
//            ->searchSelect('type','产品类型',$statusOptions)
//            ->searchBool('status','上架')
            ->buttonUpdate('setting', '修改配置')
//            ->buttonRefresh('price','重算价格')
            ->keyId()
            ->keyImage('main_img', '主图')
            ->keyText('name', '产品名称', false, 200)
            ->keyText('name_type', '充值类型')
            ->keyText('time_name', '时长')
            ->keyText('catalog_name', '分组')
            ->keyText('top_name', '顶部分类')
            ->keyDecimal('show_price', '原售价')
            ->keyDecimal('guide_price', '面值')
            ->keyDecimal('price', '结算价格')
            ->keyDecimal('sale_price', '销售价')
//                ->keyText('label1', '标签1')
//                ->keyText('label2', '标签2')

//                ->keyText('applyMsg','简介')
            ->keyText('sort', '排序')
            ->keyBool('is_sale', '销售')
            ->keyText('sale', '销量')
            ->keyMap('status', '上架', ['', '上架', '下架', '维护'])
            ->quickUpdate(['top_name', 'catalog_name', 'name_type', 'sort', 'sale_price', 'time_name', 'show_price'
//                    ,'top_name' =>['option' => ['1'=>'你好'], 'type' => 'select']
                , 'is_sale' => ['option' => ['type' => 'text'], 'type' => 'switch']
            ])
//            ->keyCreateTime()
//            ->keyUpdateTime()
            ->actionUpdate()
            ->actionDelete();
        return $builder->fetch();
    }

    public function setting()
    {
        if ($this->request->isPost()) {
            $event = new SystemConfigEvent();
            $re = $event->updateValue($this->request->post(), $this->user);
            $lists = ExchangeCouponModel::where('show_price', '<>', '')->where('discount','<',10)->select();
            foreach ($lists as $list) {
                $list['sale_price'] = $list['price'] + ($list['show_price'] - $list['price']) * floatval(config('coupon.add_price'));
                $list['back_price'] = (($list['show_price'] - $list['price']) * floatval(config('coupon.add_price'))) * (1 -floatval(config('coupon.my_amount')));
                $list->save();
            }
            $this->_returnAuto($re, false);
        } else {
            $set = SystemConfigModel::where('status', 1)->where('group', 'coupon')->where('is_dev', 0)->order('sort ASC')->select();
            return SystemConfigEvent::builderView($set, '价格计算');
        }
    }

    public function price()
    {

        $this->_returnSuccess('ok');
    }

    /**
     * 编辑分类
     * @access protected
     * @author 微尘 <yicmf@qq.com>
     */
    public function update()
    {
        if ($this->request->isPost()) { //提交表单

            if ($this->request->param('id')) {

                $data = ExchangeCouponModel::where('id', $this->request->param('id'))->find();
                $info = $this->request->except(['id']);
                $data->save($info);

                $this->_returnSuccess('更新成功');
            } else {

                $this->_returnError('数据异常');
            }

        } else {
            /* 获取分类信息 */
            $id = $this->request->get('id');
            $isEdit = $id ? true : false;
            if ($isEdit) {
                $data = ExchangeCouponModel::where('id', $id)->find();
                if (!$data) {
                    $this->_returnError('分类不存在或者已经删除');
                }
            } else {
                $data['type'] = 1;
            }
            $builder = new Edit();
            $builder->title('编辑')
                ->keyText('name', '产品名称')
                ->keyText('name_type', '充值类型')
                ->keyText('time_name', '时长')
                ->keyText('catalog_name', '分组')
                ->keyText('top_name', '顶部分类')
                ->keyImageModel('body_id', '内容')
                ->keyDecimal('show_price', '原售价')
                ->keyBool('is_sale', '是否销售')
                ->data($data)
                ->buttonSubmit('', '保存');
            //                    ->buttonClose();
            return $builder->fetch();
        }
    }
}